﻿using MC.Warehouse.Views;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml;

namespace MC.Warehouse.Core
{
    public class TabbarManager 
    {
        private const string ViewAssembly = "MC.Warehouse.Views";

        private static ObservableCollection<TabItem> _tabbarItems;
        public static ObservableCollection<TabItem> TabbarItems
        {
            get { return _tabbarItems ?? (_tabbarItems = new ObservableCollection<TabItem>()); }
        }

        private static int _selectedIndex;
        public static int SelectedIndex
        {
            get
            {
                return _selectedIndex;
            }
            set
            {
                if (_selectedIndex != value && value != -1)
                {
                    var tabItem = _tabbarItems.FirstOrDefault(p => p.IsSelected);
                    if (tabItem != null)
                    {
                        ToolbarManager.LoadToolbarItem(tabItem.Name);
                        UcSelected = tabItem.Content as BaseView;
                    }
                }
                _selectedIndex = value;
            }
        }

        public static BaseView UcSelected { get; set; }

        public static string CurrenceTabName 
        {
            get
            {
                var firstOrDefault = _tabbarItems.FirstOrDefault(p => p.IsSelected);
                return firstOrDefault != null ? firstOrDefault.Name : null;
            }
        }

        public static void OpenTab(XmlLinkedNode menuItem)
        {
            if (menuItem.Attributes == null) return;
            var ucName = menuItem.Attributes["name"];
            if (ucName == null)
            {
                MessageBox.Show(string.Format("Currence version do not support control type {0}", ucName),
                    "System", MessageBoxButton.OK);

                return;
            }
            var text = menuItem.Attributes["text"].Value;
            OpenTab(ucName.Value, text);
        }

        public static void OpenTab(string ucName, string ucCaption)
        {
            var tabItem = TabbarItems.FirstOrDefault(t => t.Name.Equals(ucName));
            if (tabItem == null)
            {
                TabbarItems.Add(new TabItem
                {
                    Name = ucName,
                    Header = ucCaption,
                    Content = CreateInstance(ucName) as BaseView,
                    IsSelected = true
                });
            }
            else
            {
                tabItem.IsSelected = true;
            }
        }

        private static RelayCommand _command;
        public static RelayCommand Command
        {
            get { return _command ?? (_command = new RelayCommand(TabExit)); }
        }

        private static void TabExit(object name)
        {
            TabbarItems.Remove(TabbarItems.FirstOrDefault(t => t.Name.Equals(name.ToString())));
        }

        internal static void Clean()
        {
            _tabbarItems = null;
            _selectedIndex = -1;
            _command = null;
            UcSelected = null;
        }

        internal static object CreateInstance(string ucName)
        {
            Type ucType = Type.GetType(string.Format("{0}.{1}", ViewAssembly, ucName));
            if (ucType == null)
            {
                MessageBox.Show(string.Format("Currence version do not support control type {0}", ucName), 
                    "System", MessageBoxButton.OK);
                return null;
            }
            return Activator.CreateInstance(ucType);
        }
    }
}
